package com.yunti.springboot.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunti.springboot.mapper.ApiLogMapper;
import com.yunti.springboot.po.ApiLog;
import com.yunti.springboot.service.ApiLogService;
import com.yunti.springboot.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 * api请求记录表 服务实现类
 * </p>
 *
 * @author ytt
 * @since 2022-03-27
 */
@Service
public class ApiLogServiceImpl extends ServiceImpl<ApiLogMapper, ApiLog> implements ApiLogService {
    @Resource
    ApiLogMapper mapper;

    @Override
    public Result getApiLogs(Integer pageNum, Integer pageSize, String userName, String state, String startTime, String endTime) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Page page = new Page<>(pageNum, pageSize);
        return Result.success(page.setRecords(mapper.selectApiLogListPage(userName, state, startTime, endTime,page)));
    }

    @Override
    public Result deleteById(Integer id) {
        if (mapper.deleteById(id) > 0)
            return Result.success("删除成功");
        return Result.error("删除失败！");
    }

    @Override
    public Result delBatchById(Integer[] ids) {
        List<Integer> list = new CopyOnWriteArrayList<>(ids);
        if (mapper.deleteBatchIds(list) > 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败！");
    }
}
